<template>
  <div>
    <div v-if="imgArr.length">
      <div
        class="demo-upload-list"
        v-for="(item, index) in imgArr"
        :key="index">
        <template>
          <img :src="item.url" ref="imgNode"/>
          <div class="demo-upload-list-cover">
            <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
            <Icon type="ios-trash-outline" @click.native="handleRemove"></Icon>
          </div>
        </template>
      </div>
    </div>
    <Upload
      v-else
      ref="upload"
      :show-upload-list="false"
      :on-success="handleSuccess"
      :format="['jpg', 'jpeg', 'png', 'gif']"
      :max-size="2048"
      :headers="headersObj"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      :before-upload="handleBeforeUpload"
      type="drag"
      :action="action"
      style="display: inline-block;width:58px;">
      <div style="width: 58px;height:58px;line-height: 58px;">
        <Icon type="ios-camera" size="20"></Icon>
      </div>
    </Upload>
    <Modal title="图片预览" v-model="visible">
      <img :src="imgUrl" v-if="visible" style="width: 100%">
    </Modal>
  </div>
</template>

<script>
import { Icon, Modal, Upload } from 'view-design'
import CONFIG from '../service/config'
import storage from '@/utils/storage'

export default {
  props: {
    value: {
      type: Array,
      default() {
        return []
      }
    }
  },
  components: {
    Icon,
    Modal,
    Upload
  },
  data() {
    return {
      imgUrl: '',
      visible: false,
    }
  },
  computed: {
    action() {
      return `${CONFIG.BASE_URL}/api/utils/upload`
    },
    imgArr: {
      get() {
        return this.value || []
      },
      set(val) {
        let value = val || []
        this.$emit('input', value)
      }
    },
    headersObj() {
      const token = this.$store.state.token || storage.getStorage('token') || ''
      return {
        Authorization: `Bearer ${token}`
      }
    }
  },
  mounted () {
  },
  methods: {
    handleBeforeUpload () {
      this.$store.commit('updateSpinShow', true)
    },
    handleView (url) {
      this.imgUrl = url
      this.visible = true
    },
    handleRemove () {
      // const fileList = this.$refs.upload.fileList v-if="$refs.img.status === 'finished'"
      // this.$refs.upload.fileList.splice(fileList.indexOf(file), 1)
      this.imgArr = []
    },
    handleSuccess (res, file) {
      this.$store.commit('updateSpinShow', false)
      console.log(res, file, 99999999)
      file.url = res.data && res.data.absoluteFilePath
      file.name = res.data && res.data.filePath
      this.imgArr = [{
        url: file.url,
        name: file.name
      }]
    },
    handleFormatError (file) {
      this.$store.commit('updateSpinShow', false)
      this.$Message.warning('请上传gif、jpg、jpeg或png格式图片')
    },
    handleMaxSize (file) {
      this.$store.commit('updateSpinShow', false)
      this.$Message.warning('上传图片尺寸不超过2M')
    }
  }
}
</script>

<style lang="less">
  
</style>